package com.api.shopapi.mapper;

import com.api.shopapi.entity.Nurse;
import com.api.shopapi.entity.NurseAide;
import com.api.shopapi.entity.dto.NurseAideDTO;
import com.api.shopapi.entity.dto.NurseDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface NurseAideMapper {

    //查询全部护工
    @Select("select n.*,u.Name,u.Phone from nurse_aide n,user u" +
            " where" +
            " u.InstitutionCode = #{institutionCode} " +
            " and n.UserCode = u.UserCode ")
    List<NurseAideDTO> getList(@Param("institutionCode") String institutionCode);

    @Insert("insert into nurse_aide " +
            "(nurseAideCode," +
            "UserCode," +
            "idCard," +
            "CreateTime," +
            "Address,"+
            "institutionCode) " +
            "values (#{nurseAide.nurseAideCode} ," +
            "#{nurseAide.UserCode} ," +
            "#{nurseAide.idCard} ," +
            "#{nurseAide.CreateTime} ," +
            "#{nurseAide.Address} ," +
            "#{nurseAide.institutionCode} )")
    int insert(@Param("nurseAide") NurseAide nurseAide);


    @Insert("<script>" +
            "insert into nurse_aide (nurseAideCode,UserCode,IdCard,CreateTime,Address,InstitutionCode) values " +

            "<foreach collection='list' item='obj' separator=','>" +
            "(#{obj.nurseAideCode},#{obj.UserCode},#{obj.IdCard}," +
            "#{obj.CreateTime},#{obj.Address},#{obj.InstitutionCode})" +
            "</foreach>" +

            "</script>")
    Integer batchInsert(List<NurseAide> list);

    @Update("update nurse_aide" +
            " set" +
            " UserCode=#{nurseAide.UserCode}," +
            " Address=#{nurseAide.Address}," +
            " idCard=#{nurseAide.idCard}" +
            " where nurseAideCode=#{nurseAide.nurseAideCode} ")
    int updateByNurseAideCode(@Param("nurseAide") NurseAide nurseAide);

    @Delete("delete from nurse_aide where NurseAideCode=#{nurseAideCode} ")
    int deleteByNurseAideCode(@Param("nurseAideCode") String nurseAideCode);

    @Select("select  * from  nurse_aide where NurseAideCode=#{nurseAideCode} limit 1")
    NurseAide getByNurseAideCode(@Param("nurseAideCode") String nurseAideCode);

    @Select("select  * from  nurse_aide where UserCode=#{userCode} limit 1")
    NurseAide getByUserCode(@Param("userCode") String userCode);


    @Select("<script>" +
            "select n.*,u.Name,u.Phone,u.Status from nurse_aide n,user u where u.InstitutionCode = #{institutionCode} " +
            "and n.UserCode = u.UserCode " +
            "<if test ='name !=null and name!=\"\" '>" +
            "and u.Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and u.Status =#{status} " +
            "</if>" +
            " order by u.CreateTime desc"+
            "</script>")
    List<NurseAideDTO> getPageList(@Param("institutionCode") String institutionCode, @Param("name") String name, @Param("status") int status);


    @Select("<script>" +
            "select * from nurse_aide " +
            " where" +
            " InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode}) " +
            " order by CreateTime desc"+
            "</script>")
    List<NurseAideDTO> listByGovCode(@Param("governmentCode") String governmentCode);


    @Select("<script>" +
            "select nurseAideCode from nurse_aide " +
            " where 1=1" +

            "<if test ='institutionCode == \"\" '>" +
            " and InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} )" +
            "</if>" +
            "<if test ='institutionCode != \"\" '>" +
            " and institutionCode = #{institutionCode} " +
            "</if>" +

            " order by CreateTime desc"+
            "</script>")
    List<NurseAide> listByGovCodeInsCode(@Param("governmentCode") String governmentCode,
                                     @Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select * from nurse_aide " +
            " where" +
            " institutionCode=#{institutionCode} " +
            "</script>")
    List<NurseAideDTO> listByInsCode(@Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select IdCard from nurse_aide where IdCard in" +
            "<foreach collection='list' item='item' open='(' separator=',' close=')' >" +
            "#{item.IdCard}"+
            "</foreach>" +
            "</script>")
    List<String> batchSelectIdCard(List<NurseAide> list);


}
